﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace MultiCastSender
{
    class Program
    {
        static void Main(string[] args)
        {
            // Socket Creation
            Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            // Target to send the datas
            EndPoint multicastEndPoint = new IPEndPoint(IPAddress.Parse("239.1.1.42"), 50000);

            Console.WriteLine("Press any key to send a message");
            Console.WriteLine("Press the key 'q' to quit");

            ConsoleKeyInfo key;
            string HelloString = "Hello";
            string QuitString = "Quit";

            do
            {
                key = Console.ReadKey();
                if (key.Key != ConsoleKey.Q)
                {
                    sendSocket.SendTo(Encoding.UTF8.GetBytes(HelloString), HelloString.Length, SocketFlags.None, multicastEndPoint);
                }
                else
                {
                    sendSocket.SendTo(Encoding.UTF8.GetBytes(QuitString), QuitString.Length, SocketFlags.None, multicastEndPoint);
                }
            } while (key.Key != ConsoleKey.Q);
        }
    }
}

